##
## 4 f r
## 103 106 25
# aesthetic mapping gives multiple groups for each bar
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar()
print(p1)# stacked, but need to adjust color transparency, which is "alpha"
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar(alpha = 0.3, position="identity")
print(p1)# better to use position = fill for stacking, but with equivalent height
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar(position="fill")
print(p1)# best to use position = dodge for multiple bars
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar(position="dodge",color="black",size=1)
print(p1)# more typical "bar plot" has heights as the values themselves
dTiny <- tapply(X=d$hwy,INDEX=as.factor(d$fl),FUN=mean) #calculate the means
dTiny <- data.frame(hwy=dTiny) # create a single-column data frame
dTiny <- cbind(fl=row.names(dTiny),dTiny) #
p2 <- ggplot(dTiny, aes(x=fl,y=hwy,fill=fl)) +
geom_col()
print(p2)# basic boxplot is simple and informative
p1 <- ggplot(d,aes(x=fl,y=hwy,fill=fl)) +
geom_boxplot()
print(p1)# now overlay the raw data
p1 <- ggplot(d,aes(x=fl,y=hwy)) +
geom_boxplot(fill="thistle",outlier.shape=NA) +
# geom_point()
geom_point(position=position_jitter(width=0.1,height=0.7),color="grey60",size=2)
print(p1) ### Using Colors
d <- mpg
# --------- discrete classification
# scale_fill_manual for boxplots,bars
# scale_color_manual for points, lines
# boxplot no color
p_fil <- ggplot(d,aes(x=as.factor(cyl),y=cty))
p_fil + geom_boxplot()# boxplot default ggplot fill
p_fil <- ggplot(d,aes(x=as.factor(cyl),y=cty,fill=as.factor(cyl))) + geom_boxplot()
plot(p_fil)# create custom color palette
my_cols <- c("red","brown","blue","orange")
# boxplot with custom colors for fill
p_fil + scale_fill_manual(values=my_cols)# scatterplot default ggplot colors
p_col <- ggplot(d,aes(x=displ,y=cty,col=as.factor(cyl))) + geom_point(size=3)
plot(p_col)# continuous classification (color gradient)
# default color gradient
p_grad <- ggplot(d,aes(x=displ,y=cty,col=hwy)) + geom_point(size=3)
plot(p_grad)# custom diverging gradient (3-colors)
mid <- median(d$cty)
p_grad + scale_color_gradient2(midpoint=mid,
low="blue",
mid="white",
high="red")d <- mpg # use built in mpg data frame
p1 <- ggplot(d,aes(x=fl,y=hwy,group=fl))
p1 + geom_boxplot() # no color# create a palette
myColors <- c("red","green","pink","blue","orange") #no info
p1 + geom_boxplot(fill=myColors)# using alpha transparency for histograms
x1 <- rnorm(n=100,mean=0)
x2 <- rnorm(n=100,mean=3)
dFrame <- data.frame(v1=c(x1,x2))
lab <- rep(c("Control","Treatment"),each=100)
dFrame <- cbind(dFrame,lab)
str(dFrame)## 'data.frame': 200 obs. of 2 variables:
## $ v1 : num -0.0306 1.4022 0.166 2.5987 -1.1781 ...
## $ lab: Factor w/ 2 levels "Control","Treatment": 1 1 1 1 1 1 1 1 1 1 ...
h1 <- ggplot(dFrame,aes(x=v1,fill=lab))
h1 + geom_histogram(position="identity",alpha=0.5,color="black") # use scale fill manual
p2 <- ggplot(d,aes(x=fl,y=hwy,fill=fl)) + geom_boxplot()+ scale_fill_manual(values=wes_palettes[["Darjeeling1"]])
print(p2)myCanvas <- canva_palettes[["Fresh and bright"]]
myCanvas <- c(gray(0.6),myCanvas)
p2 + geom_boxplot() + scale_fill_manual(values=myCanvas)## Scale for 'fill' is already present. Adding
## another scale for 'fill', which will replace
## the existing scale.
# slightly different scheme for points, which have color
p3 <- ggplot(d,aes(x=displ,y=hwy,color=fl)) +
geom_point() +
scale_color_brewer(palette="Spectral")
print(p3)# use scale_color_gradient to change low and high colors
# scale_color_gradient(
p3 +
scale_color_gradient(low="red",high="blue")# use scale_color_gradient2 for a 3 color gradient
z=mean(d$cty)
p3 +
scale_color_gradient2(midpoint=z,low="red",mid="seagreen",high="cyan",space="Lab")## xVar yVar
## 1 1 1
## 2 2 1
## 3 3 1
## 4 4 1
## 5 5 1
## 6 6 1
## xVar yVar zVar
## 1 1 1 1.93
## 2 2 1 4.78
## 3 3 1 4.30
## 4 4 1 3.63
## 5 5 1 6.51
## 6 6 1 5.77
p4 <- ggplot(myData,aes(x=xVar,y=yVar,fill=zVar))
p4 + geom_tile() +
scale_fill_gradient2(midpoint=19,low="brown",mid=grey(0.8),high="darkblue") #### New viridis color scale
# options viridis, cividis, magma, inferno, plasma
p4 + geom_tile() + scale_fill_viridis_c(option="inferno")